package org.jeecg.modules.demo.crm.controller;


import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.demo.crm.email.EmailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Api(tags="")
@RestController
@RequestMapping("/crm/email")
@Slf4j
public class EmailController {

    @Autowired
    private JavaMailSender qqMailSender;

    @Autowired
    private JavaMailSender neteaseMailSender;

    @Value("${qq.mail.username}")
    private String qqFrom;

    @Value("${netease.mail.username}")
    private String neteaseFrom;

    // 发送 QQ 邮箱的简单文本邮件
    @PostMapping("/sendQQMail")
    @IgnoreAuth
    public String sendQQMail(@RequestParam String to, @RequestParam String subject, @RequestParam String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(qqFrom);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        qqMailSender.send(message);
        return "QQ 邮箱邮件发送成功";
    }

    // 发送网易邮箱的简单文本邮件
    @PostMapping("/sendNeteaseMail")
    @IgnoreAuth
    public String sendNeteaseMail(@RequestParam String to, @RequestParam String subject, @RequestParam String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(neteaseFrom);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        neteaseMailSender.send(message);
        return "网易邮箱邮件发送成功";
    }

    // 发送网易邮箱的 HTML 邮件
    @PostMapping("/sendNeteaseHtmlMail")
    @IgnoreAuth
    public String sendNeteaseHtmlMail(@RequestParam String to,
                                      @RequestParam String subject,
                                      @RequestParam String content,
                                      @RequestParam String attachmentUrl) throws MessagingException {
        subject = "货架报价提醒";
        content = "<p>宜兴仓库新增清洁设备工程项目--小货架（35组）报价:</p><img src='http://154.201.66.67:9000/blithe-crm/temp/发票_1750916183628.png'/>";

        return sendHtmlMail(neteaseMailSender, neteaseFrom, to, subject, content,attachmentUrl);
    }

    private String sendHtmlMail(JavaMailSender mailSender,
                                String from,
                                String to,
                                String subject,
                                String content,
                                String attachmentUrl)
            throws  MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // 指定编码为 UTF-8

        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);


        // 读取模板文件并替换占位符
        String htmlContent = EmailUtils.getHtmlContent(subject, content);
        helper.setText(htmlContent, true);

        // 如果提供了附件 URL，则下载文件并添加为附件
        if (attachmentUrl != null && !attachmentUrl.isEmpty()) {
            String[] fileUrlS = attachmentUrl.split(",");
            for (String fileUrl : fileUrlS) {
                File attachmentFile = EmailUtils.downloadFile(fileUrl);
                if (attachmentFile != null) {
                    FileSystemResource file = new FileSystemResource(attachmentFile);
                    helper.addAttachment(file.getFilename(), file);
                }
            }

        }
        mailSender.send(mimeMessage);
        return "HTML 邮件发送成功";
    }

}
